from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from base.basepage import PageBase
from common.UiSelector import UiSelector
from page.main_page import MainPage


class EditPage(PageBase):
    TIME_RULER_ID = 'timeRuler'  # 时间线
    CANVAS_ID = 'operation_layout'  # 画布
    TOOLS_TAB_ID = 'com.lemon.lv:id/ttvTitle'
    EXIT_DRAFT_ID = 'tvBack'
    SEARCH_INPUT_ID = 'com.lemon.lv:id/input'
    OK_BTN_ID = 'newBtnOk'
    PLAY_PAUSE_ID = 'ivPlay'

    def tools_tab(self, value):
        return self.find_text(value, (UiSelector.ID, self.TOOLS_TAB_ID))

    def swipe_time_ruler(self, seconds: int):
        el = self.locator((By.ID, self.TIME_RULER_ID))
        x = el.location['x'] + el.size['width'] / 2
        y = el.location['y'] + el.size['height'] / 2
        one_seconds = el.size['width'] / 8 * 1.2
        self.flick(x + one_seconds * seconds, y, x, y)

    def exit_editor(self):
        self.click((By.ID, self.EXIT_DRAFT_ID))
        WebDriverWait(self.driver, 10).until(
            ec.visibility_of_all_elements_located((By.ID, MainPage.DRAFT_MANAGER_ID)))

    def search_input(self, text):
        self.input([
            (UiSelector.ID, 'com.lemon.lv:id/extraContainer'), (UiSelector.ID, 'com.lemon.lv:id/input')],
                   text,1)
